#!/bin/bash -e

TOP_DIR=$(pwd)
ROOTFS_PATH=$(realpath fs_debian_*)

echo Making rootfs!
echo "ROOTFS_PATH=$ROOTFS_PATH"

ROOTFSIMAGE=$TOP_DIR/rootfs.img
EXTRA_SIZE_MB=300

echo "I: 打包文件系统"
IMAGE_SIZE_MB=$(expr $(echo $(sudo du -sh -m $(realpath "$ROOTFS_PATH") | cut -f1) + ${EXTRA_SIZE_MB}))

# 删除历史镜像
if [ -e ${ROOTFSIMAGE} ]; then
	rm ${ROOTFSIMAGE}
fi

# 生成IMG镜像文件
dd if=/dev/zero of=${ROOTFSIMAGE} bs=1M count=0 seek=${IMAGE_SIZE_MB} &> /dev/null

sudo mkfs.ext4 -d ${ROOTFS_PATH} ${ROOTFSIMAGE} &> /dev/null

echo Rootfs Image: ${ROOTFSIMAGE}

